package com.atguigu.boot;

import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import com.atguigu.boot.config.BeanConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Arrays;

/**
 * @author : wenchao.long
 * @date : Created in 2021/02/02 13:32 周二
 * @description :
 *         <p>
 *         {@link SpringBootApplication} 标注这是一个Spring应用
 *         </p>
 */
@SpringBootApplication
@Slf4j
public class MainApplication {

    public static void main(String[] args) {
        // 1. 返回IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        // 2. 查看容器里面注册的所有组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            log.info("组件------" + name);
        }

        String[] beanNamesForType = run.getBeanNamesForType(CacheAspectSupport.class);
        log.info("-------------------CacheAspectSupport组件" + Arrays.toString(beanNamesForType) + beanNamesForType.length);

        // 3. 从容器中获取组件
        Pet p1 = run.getBean("tom", Pet.class);
        Pet p2 = run.getBean("tom", Pet.class);

        log.info("从容器中获取组件----------" + (p1 == p2));

        // com.atguigu.boot.config.BeanConfig$$EnhancerBySpringCGLIB$$5593470b@54acff7d
        BeanConfig beanConfig = run.getBean(BeanConfig.class);

        log.info("BeanConfig-----------" + beanConfig);


        // 4. 当配置类的 proxyBeanMethods 为true时 用代理对象调用方法 看看是否是同一个对象
        User u1 = beanConfig.user01();
        User u2 = beanConfig.user01();

        log.info("proxyBeanMethods == true --> 同一个Bean: " + (u1 == u2));

        boolean h1 = run.containsBean("haha");

        log.info("读取xml文件 --------" + h1);

        boolean h2 = run.containsBean("hehe");

        log.info("读取xml文件 --------" + h2);

    }
}
